package com.yingsheng.bi.hks;

import com.yingsheng.bi.hks.basic.enums.XjyOssEnum;
import com.yingsheng.bi.hks.mapping.ServiceDetailMgmtMappings;
import com.yingsheng.bi.hks.repository.dao.po.ServiceDetail;
import com.yingsheng.bi.hks.repository.dao.service.ServiceDetailDaoService;
import io.minio.ObjectWriteResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintViolationException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class MinioUploadTestService {

    @Autowired
    private ServiceDetailDaoService serviceDetailDaoService;

    ;

    @Test
    public void upload() {

        File dir = new File("D:\\images\\850283c46cab4c47a68367a22a01106e");
        System.out.println(dir.getName());
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            assert files != null;
            List<String> objectList = new ArrayList<>();
            ServiceDetail serviceDetail = new ServiceDetail();
            for (File file : files)  {
                if (file.isFile()) {
                    String fileName = file.getName();
                    ObjectWriteResponse response = XjyOssEnum.SERVICE_IMAGE.putObject(file, fileName);
                    String objectName = objectName(response.object());
                    System.out.println(objectName);
                    if (fileName.contains("_t")) {
                        serviceDetail.setTopimages(objectName);
                    } else {
                        objectList.add(objectName);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(objectList)) {
                String images = StringUtils.collectionToCommaDelimitedString(objectList);
                serviceDetail.setId(dir.getName());
                serviceDetail.setImages(images);
                serviceDetailDaoService.updateById(serviceDetail);
            }
        }
        System.out.println("文件传输完毕");

    }

    private String objectName(String objectName) {
        String prefix = "http://static.nbyingchuang.cn:9000/miniapp-xjy-dev";
        return prefix + objectName;
    }

    @Test
    public void testMethodValidation() {
        String id = "";
        try {
            serviceDetailDaoService.updateById(id);
        } catch (ConstraintViolationException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("11111");
    }


    @Test
    public void uploadOne() {

        File file = new File("D:\\video\\xuanchuanpian.mp4");
        ObjectWriteResponse response = XjyOssEnum.GOODS_IMAGE.putObject(file, file.getName());
        String objectName = objectName(response.object());
        String bucketName = response.bucket();
        String url = "http://cdn.nbyingchuang.cn" + bucketName + objectName;
        System.out.println(objectName);
       // http://cdn.nbyingchuang.cn/miniapp-xjy-dev/public/home/xuanchuanpian.mp4
        System.out.println("文件传输完毕");

    }
}
